package com.lisx.module_note.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.fenghuajueli.lib_data.entity.db.MemoDbEntity;
import com.fenghuajueli.lib_data.entity.db.NoteDbEntity;
import com.lisx.module_note.databinding.ItemNoteListBinding;
import com.lisx.module_note.util.TimeUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class NoteListAdapter extends RecyclerView.Adapter<NoteListAdapter.MyViewHolder> {

    List<NoteDbEntity> noteDbEntities = new ArrayList<>();

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemNoteListBinding binding = ItemNoteListBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new MyViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        NoteDbEntity entity = noteDbEntities.get(position);
        long recordTime = entity.getRecord_time();
        holder.binding.tvYear.setText(TimeUtils.parseYear(recordTime) + "年");
        holder.binding.tvMonth.setText("/" + TimeUtils.parseMonth(recordTime));
        holder.binding.tvDay.setText(TimeUtils.parseDay(recordTime));
        holder.binding.tvMsg.setText(entity.getMsg());
        holder.binding.tvTime.setText(TimeUtils.parseHMTimeLong(recordTime / 1000));
        if (entity.getIs_hide()) {
            holder.binding.tvYear.setVisibility(View.GONE);
        } else {
            holder.binding.tvYear.setVisibility(View.VISIBLE);
        }
        holder.binding.ivEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onEdit(entity);
                }
            }
        });

        holder.binding.ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onDelete(entity);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return noteDbEntities.size();
    }

    public void setData(List<NoteDbEntity> list) {
        Collections.sort(list, new Comparator<NoteDbEntity>() {
            @Override
            public int compare(NoteDbEntity o1, NoteDbEntity o2) {
                return (int) (o1.getRecord_time() - o2.getRecord_time());
            }
        });
        //判断是否隐藏时间
        for (int i = 0; i < list.size(); i++) {
            NoteDbEntity noteDbEntity1 = list.get(i);
            if (i + 1 < list.size()) {
                NoteDbEntity noteDbEntity2 = list.get(i + 1);
                boolean sameYear = TimeUtils.isSameYear(noteDbEntity1.getRecord_time(), noteDbEntity2.getRecord_time());
                if (sameYear) {
                    noteDbEntity2.setIs_hide(true);
                }
            }
        }
        this.noteDbEntities.clear();
        this.noteDbEntities.addAll(list);
        notifyDataSetChanged();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        private final ItemNoteListBinding binding;

        public MyViewHolder(@NonNull ItemNoteListBinding itemView) {
            super(itemView.getRoot());
            binding = itemView;
        }
    }

    public interface OnItemClickListener {
        void onEdit(NoteDbEntity noteDbEntity);

        void onDelete(NoteDbEntity noteDbEntity);
    }

    OnItemClickListener listener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
}
